package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.mappers.UserMapper;
import com.tanhua.model.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@Slf4j
@DubboService
public class UserApiImpl  implements UserApi{

    @Autowired
    private UserMapper userMapper;

    //根据手机号码查询用户
    public User findByMobile(String mobile) {
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("mobile",mobile);
        return userMapper.selectOne(qw);
    }

    @Override
    public Long save(User user) {
        userMapper.insert(user);
        return user.getId();
    }

    @Override
    public void update(User user) {
        log.info("保存环信用户数据 ===={}",user.toString());
        userMapper.updateById(user);
    }

    @Override
    public User findByHuanxin(String huanxinId) {
        System.out.println("huanxinId = " + huanxinId.toLowerCase());
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("hx_user", huanxinId.toLowerCase()));
        log.info("查询到的环信用户数据 ===={}",user);
        return user;
    }

    @Override
    public User findById(Long id) {
        return userMapper.selectById(id);
    }

    @Override
    public User findByHxId(String hxId) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("hx_user",hxId);
        return userMapper.selectOne(userQueryWrapper);
    }
}
